package edu.iscas.appcheckclient.model.actions;


import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import java.io.Reader;
import java.util.LinkedList;
import java.util.List;

import edu.iscas.appcheckclient.util.GsonUtil;

public abstract class Action {

    protected String name;

    protected String statement;

    public static Action fromJSON(Reader reader){
        return (Action) GsonUtil.buildGson().fromJson(reader, Action.class);
    }

    public static Action fromJSON(String jsonString){
        return (Action) GsonUtil.buildGson().fromJson(jsonString, Action.class);
    }

    public static List<Action> listFromJSON(String jsonString) {
        return (List) GsonUtil.buildGson().fromJson(jsonString, new Action.ActionTypeToken().getType());
    }

    public static String listToJSON(List<Action> paramList) {
        return GsonUtil.buildGson().toJson(paramList);
    }

    public abstract String brewStatement(boolean paramBoolean);

    public abstract void setStatement(String statement);

    public abstract String getTypeString();

    public abstract String getValue();

    public String toJSON(){
        return GsonUtil.buildGson().toJson(this);
    }

    public JsonElement toJsonElement(){
        return GsonUtil.buildGson().toJsonTree(this);
    }

    public static final class ActionTypeToken extends TypeToken<LinkedList<Action>>{}

    public String toString(){
        String str = getValue();
        StringBuilder stringBuilder = new StringBuilder().append(getTypeString());
        if(str.length() > 0){
            str = new StringBuffer().append(": ").append(str).toString();
            return stringBuilder.append(str).toString();
        }
        return null;
    }


}
